<?php

class aoiReservationStatusWidget extends sfWidgetForm
{
  public function configure($options = array(), $attributes = array())
  {
 
    $this->addOption('choices', array(
      'created' => 'Created',
      'pending' => 'Pending',
      'validated' => 'Validated',
      'cancelled' => 'Cancelled'
    ));
  }
 
  public function render($name, $value = null, $attributes = array(), $errors = array())
  {
    $reservationStatus = '';

    $value = $value === null ? 'null' : $value;
 
    $options = array();
    foreach ($this->getOption('choices') as $key => $option)
    {
      $attributes = array('value' => self::escapeOnce($key));
      if ($key == $value)
      {
        $attributes['selected'] = 'selected';
        $reservationStatus = $value;
      }
 
      $options[] = $this->renderContentTag(
        'option',
        self::escapeOnce($option),
        $attributes
      );
    }
 
    /*
    return $this->renderContentTag(
      'select',
      "\n".implode("\n", $options)."\n",
      array_merge(array('name' => $name), $attributes
    ));*/

    if($reservationStatus == 'created')
    {
      $html = '<input type="hidden" name="aoi_itinerary_reservation[status]" id="aoi_itinerary_reservation_status" value="'.$reservationStatus.'">'
              .'En attente de validation par un agent. <button onclick="'
              .'var amount = parseFloat($(\'#aoi_itinerary_reservation_amount\').val());'
              .'if(!(amount > 0)){alert(\'Veuillez indiquer un montant pour la reservation\'); return false;}'
              .'if(confirm(\'Envoyer le mail de paiement pour le montant de \'+amount+\' EUR ? \'))'
              .' {'
              .'   $(\'#aoi_itinerary_reservation_status\').val(\'pending\');'
              .' } else return false;"'
              .' >valider et envoyer le mail de paiement</button>';
      return $html;
    }
    else return '<input type="hidden" name="aoi_itinerary_reservation[status]" id="aoi_itinerary_reservation_status" value="'.$reservationStatus.'"> '.$reservationStatus;
  }
}